package com.xuerhua.yxt.controller.teacher;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xuerhua.yxt.common.AppUser;
import com.xuerhua.yxt.common.Const;
import com.xuerhua.yxt.common.MenuConst;
import com.xuerhua.yxt.common.RspResult;
import com.xuerhua.yxt.common.enums.FunctionRoomStatus;
import com.xuerhua.yxt.common.enums.Status;
import com.xuerhua.yxt.common.enums.WorkApprovalType;
import com.xuerhua.yxt.controller.BaseController;
import com.xuerhua.yxt.model.FunctionRoomApply;
import com.xuerhua.yxt.model.Grid;
import com.xuerhua.yxt.page.OrderBy;
import com.xuerhua.yxt.page.Page;
import com.xuerhua.yxt.reqVo.FunctionRoomApplyReqVO;
import com.xuerhua.yxt.reqVo.FunctionRoomQueryVO;
import com.xuerhua.yxt.service.IFunctionRoomApplyService;
import com.xuerhua.yxt.service.IWorkApprovalConfService;
import com.xuerhua.yxt.utils.BeanUtils;
import com.xuerhua.yxt.utils.DateUtil;
import com.xuerhua.yxt.view.FunctionRoomApplyView;

@RequestMapping(Const.TEACHER_ROLE +  MenuConst.FUNCTION_ROOM)
@Controller
public class FunctionRoomController extends BaseController {

	private Logger logger = LoggerFactory.getLogger(FunctionRoomController.class);
	
	@Autowired
	private IWorkApprovalConfService workApprovalConfService;
	@Autowired
	private IFunctionRoomApplyService functionRoomApplyService;
	
	@RequestMapping(value={"","/"})
	public String functionRoom(Model model) {
		AppUser appUser = getAppUser();
		if(workApprovalConfService.isWorkApprover(
				WorkApprovalType.ROOM_APPROVAL.getCode(), 
				appUser.getTeacher().getId(), appUser.getTeacher().getSchoolId())) {
			return "functionRoom/approverList";
		}
		return "functionRoom/list";
	}
	
	@RequestMapping("apply")
	public String addApply(Model model) {
		return "functionRoom/apply";
	}
	
	/**
	 * 工作审批详情页
	 * @param applyId
	 * @return
	 */
	@RequestMapping("detail/{applyId}")
	public String detail(Model model, @PathVariable Integer applyId) {
		FunctionRoomApplyView view = functionRoomApplyService.
				selectViewById(applyId);
		model.addAttribute("isApprovaler", workApprovalConfService.
				isWorkApprover(WorkApprovalType.ROOM_APPROVAL.getCode(), 
						getAppUser().getTeacher().getId(), getAppUser().getTeacher().getSchoolId()));
		model.addAttribute("detail", view);
		if(DateUtil.addDay(new Date(), -1).getTime() < view.getUseDate().getTime()) {
			model.addAttribute("isEdit", true);
		}
		return "functionRoom/detail";
	}
	
	@RequestMapping("successList")
	public String successList() {
		return "functionRoom/successList";
	}
	
	/**
	 * 分页查询工作审批
	 * @param vo
	 * @return
	 */
	@RequestMapping("page/query")
	@ResponseBody Object pageQuery(FunctionRoomQueryVO vo) {
		try {
			Page<FunctionRoomApply> page = new Page<>();
			Grid grid = new Grid();
			List<OrderBy> sorts = new ArrayList<>();
			vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
			sorts.add(new OrderBy("CREATE_TIME", "desc"));
			page.setSorts(sorts);
			page.setParams(vo);
			page.setPageNo(vo.getPage());
			page.setPageSize(vo.getRows());
			List<FunctionRoomApply> list = functionRoomApplyService.selectListByPage(page);
			grid.setTotal(page.getTotalRecord());
			grid.setRows(BeanUtils.copyAs(list, FunctionRoomApplyView.class));
			return new RspResult(Status.SUCCESS, grid);
		} catch (Exception e) {
			logger.error("--pageQuery error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	@RequestMapping("getAuditSuccessListByDate")
	@ResponseBody Object abc(FunctionRoomQueryVO vo) {
		vo.setSchoolId(getAppUser().getTeacher().getSchoolId());
		List<FunctionRoomApply> list = functionRoomApplyService.selectAuditSuccessListByDate(vo);
		return BeanUtils.copyAs(list, FunctionRoomApplyView.class);
	}
	
	//工作审批提交
	@RequestMapping("apply/submit")
	@ResponseBody Object applySubmit(FunctionRoomApplyReqVO reqVo) {
		try {
			AppUser appUser = getAppUser();
			FunctionRoomApply record = BeanUtils.copyAs(reqVo, FunctionRoomApply.class);
			record.setUseDate(DateUtil.strToDate(reqVo.getUseDateStr(), DateUtil.DEFAULT_DATE_FORMAT3));
			record.setApplyer(appUser.getTeacher().getName());
			record.setApplyerId(appUser.getTeacher().getId());
			record.setStatus(FunctionRoomStatus.PENDING.getCode());
			record.setSchoolId(appUser.getTeacher().getSchoolId());
			functionRoomApplyService.addApply(record);
			return new RspResult(Status.SUCCESS);
		} catch (Exception e) {
			logger.error("----applySubmit error={}", e);
		}
		return new RspResult(Status.FAIL);
	} 
	
	/**
	 * 审批成功
	 * @param reqVo
	 * @return
	 */
	@RequestMapping("approval/success")
	@ResponseBody Object approvalSuccess(FunctionRoomApplyReqVO reqVo) {
		try {
			reqVo.setStatus(FunctionRoomStatus.AGREE.getCode());
			return functionRoomApplyService.updateAudit(reqVo, 
					getAppUser().getTeacher());
		} catch (Exception e) {
			logger.error("---approvalSuccess error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	/**
	 * 审批驳回
	 * @param reqVO
	 * @return
	 */
	@RequestMapping("approval/reject")
	@ResponseBody Object approvalReject(FunctionRoomApplyReqVO reqVO) {
		try {
			reqVO.setStatus(FunctionRoomStatus.REJECT.getCode());
			return functionRoomApplyService.updateAudit(reqVO, 
					getAppUser().getTeacher());
		} catch (Exception e) {
			logger.error("---reject error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	/**
	 * 申请人取消
	 * @param reqVO
	 * @return
	 */
	@RequestMapping("applyer/cancel")
	@ResponseBody Object applyerCancel(FunctionRoomApplyReqVO reqVO) {
		try {
			reqVO.setStatus(FunctionRoomStatus.APPLYER_CANCEL.getCode());
			return functionRoomApplyService.updateAudit(reqVO, 
					getAppUser().getTeacher());
		} catch (Exception e) {
			logger.error("---applyerCancel error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
	
	/**
	 * 撤消审批
	 * @param reqVO
	 * @return
	 */
	@RequestMapping("approval/cancel")
	@ResponseBody Object approvalCancel(FunctionRoomApplyReqVO reqVO) {
		try {
			reqVO.setStatus(FunctionRoomStatus.APPROVER_CANCEL.getCode());
			return functionRoomApplyService.updateAudit(reqVO, 
					getAppUser().getTeacher());
		} catch (Exception e) {
			logger.error("---applyerCancel error={}", e);
		}
		return new RspResult(Status.FAIL);
	}
}
